#!/bin/bash
# Windows Process Spy - Clean Script (Bash version)

echo "Windows Process Spy - Clean Tool"
echo "================================="
echo ""
echo "Select clean operation:"
echo "1. Clean build cache (recommended)"
echo "2. Clean release files"
echo "3. Clean all files (dangerous)"
echo "4. Exit"
echo ""

read -p "Enter your choice (1-4): " choice

case $choice in
    1)
        echo ""
        echo "Cleaning build cache..."
        
        # 删除构建目录
        [ -d "build" ] && rm -rf build && echo "Removed: build/"
        [ -d "dist" ] && rm -rf dist && echo "Removed: dist/"
        [ -d "release" ] && rm -rf release && echo "Removed: release/"
        
        # 删除所有 __pycache__ 目录
        find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null
        echo "Removed: __pycache__/ directories"
        
        # 删除 .pyc 和 .pyo 文件
        find . -type f -name "*.pyc" -delete 2>/dev/null
        find . -type f -name "*.pyo" -delete 2>/dev/null
        echo "Removed: *.pyc, *.pyo files"
        
        # 删除 .spec 文件
        find . -type f -name "*.spec" -delete 2>/dev/null
        echo "Removed: *.spec files"
        
        # 删除日志文件
        find . -type f -name "*.log" -delete 2>/dev/null
        echo "Removed: *.log files"
        
        echo ""
        echo "[SUCCESS] Build cache cleaned!"
        ;;
        
    2)
        echo ""
        echo "Cleaning release files..."
        
        [ -d "build" ] && rm -rf build && echo "Removed: build/"
        [ -d "dist" ] && rm -rf dist && echo "Removed: dist/"
        [ -d "release" ] && rm -rf release && echo "Removed: release/"
        
        echo ""
        echo "[SUCCESS] Release files cleaned!"
        ;;
        
    3)
        echo ""
        echo "WARNING: This will delete all files except core files!"
        read -p "Are you sure? (y/N): " confirm
        
        if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
            echo "Operation canceled"
        else
            echo ""
            echo "Cleaning all files..."
            
            # 保留的核心文件列表
            KEEP_FILES=(
                "main.py"
                "cli.py"
                "requirements.txt"
                "clean.bat"
                "clean.sh"
                "build_universal.py"
                "build.bat"
                "build.sh"
                "pyproject.toml"
                "PROJECT_GUIDE.md"
                "screenshot.bat"
                "setup_uv.bat"
                ".gitignore"
                ".python-version"
            )
            
            # 保留的目录列表
            KEEP_DIRS=(
                "modules"
                "tests"
                ".venv"
                "res"
                ".git"
                ".idea"
            )
            
            echo "Keeping core files..."
            for file in "${KEEP_FILES[@]}"; do
                [ -f "$file" ] && echo "  Keep: $file"
            done
            
            echo ""
            echo "Keeping core directories..."
            for dir in "${KEEP_DIRS[@]}"; do
                [ -d "$dir" ] && echo "  Keep: $dir/"
            done
            
            echo ""
            echo "Deleting other files..."
            
            # 删除不在保留列表中的文件
            for file in *; do
                if [ -f "$file" ]; then
                    keep=false
                    for keep_file in "${KEEP_FILES[@]}"; do
                        if [ "$file" = "$keep_file" ]; then
                            keep=true
                            break
                        fi
                    done
                    if [ "$keep" = false ]; then
                        rm -f "$file"
                        echo "  Deleted: $file"
                    fi
                fi
            done
            
            # 删除不在保留列表中的目录
            for dir in */; do
                dir="${dir%/}"  # 移除末尾的斜杠
                keep=false
                for keep_dir in "${KEEP_DIRS[@]}"; do
                    if [ "$dir" = "$keep_dir" ]; then
                        keep=true
                        break
                    fi
                done
                if [ "$keep" = false ]; then
                    rm -rf "$dir"
                    echo "  Deleted: $dir/"
                fi
            done
            
            echo ""
            echo "[SUCCESS] All files cleaned!"
        fi
        ;;
        
    4)
        echo "Exiting..."
        exit 0
        ;;
        
    *)
        echo "Invalid choice, please run the script again"
        exit 1
        ;;
esac

echo ""
echo "Clean operation completed!"
read -p "Press Enter to exit..."
